﻿using System;
using System.Collections.Generic;

namespace AppMain
{
    public class TMRunProcesser : BaseMoveProcesser
    {
        protected override void onEnter(RoleMove_Status fromStatus)
        {

        }

        protected override void onUpdate()
        {

        }

        protected override void onExit(RoleMove_Status toStatus)
        {

        }

        protected override void onTick(float frameTime)
        {
            float xDistance = this.data.dir.x * frameTime;
            float zDistance = this.data.dir.z * frameTime;
            BaseScene scene = this.agent.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value;
            Vec2 result = Physics.moveCast(this.agent.box3D, scene.colliderSegments, xDistance, zDistance);
            if (result == Vec2.zero)
            {
                result = Physics.moveCast(this.agent.box3D, scene.colliderSegments, 0, zDistance);
                if (result == Vec2.zero)
                {
                    result = Physics.moveCast(this.agent.box3D, scene.colliderSegments, xDistance, 0);
                }
            }
            xDistance = result.x;
            zDistance = result.y;
            this.agent.move(xDistance, zDistance);
            //Console.WriteLine(string.Format("{0}     {1}     {2}", this.data.dir.x, frameTime, xDistance));
            //广播给场景中玩家
            if (scene != null)
            {
                pb.RoleMoveBroadCastMsg msg = new pb.RoleMoveBroadCastMsg();
                msg.roleId = this.agent.UID;
                msg.status = this.agent.AttrMap.getAttr<int>(RoleAttr.LookFlag).Value;
                msg.posx = this.agent.box3D.posX;
                msg.posy = this.agent.box3D.posY;
                msg.posz = this.agent.box3D.posZ;
                msg.dirx = this.data.dir.x;
                msg.dirz = this.data.dir.z;
                scene.broadCast(Cmd.RoleMoveBroadCastMsg, msg);
            }
        }

        protected override void onDispose()
        {

        }

    }
}
